package com.lyhdev.android.touchenglish;

import android.media.MediaPlayer;

public class QuizSoundPlayer implements Runnable {

	private MediaPlayer __mplayer = null;

	private boolean __alived = true;

	private TEQuiz __quiz;

	public QuizSoundPlayer(TEQuiz quiz) {
		__quiz = quiz;
	}

	public void stop() {
		if (__mplayer != null) {
			if (__mplayer.isPlaying()) {
				__mplayer.stop();
			}
			__mplayer.release();
		}
		__alived = false;
	}

	public void run() {
		try {
			if (!__quiz.isSoundCached()) {
				__quiz.cacheSound();
			}
			if (__quiz.getSoundFile().exists() && __alived) {
				String soundPath = __quiz.getSoundFile().getAbsolutePath();
				__mplayer = new MediaPlayer();
				__mplayer.setDataSource(soundPath);
				__mplayer.prepare();
				__mplayer.start();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
